<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="10">
        <el-col :span="7">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="queryInfo.query"
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showAddGoodsDialog"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="700"
        ></el-table-column>
        <el-table-column
          label="商品价格"
          prop="goods_price"
          width="100"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="100"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="200"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >操作</el-button
            >
            <el-button type="danger" size="mini" icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加商品对话框 -->
    <el-dialog
      title="添加商品"
      :visible.sync="addGoodsdialogVisible"
      width="40%"
      @close="addGoodsDialogColsed"
    >
      <el-form
        :model="addGoodsForm"
        ref="addGoodsFormRef"
        :rules="addGoodsFormRules"
      >
        <el-form-item label="商品名称:" prop="goods_name">
          <el-input v-model="addGoodsForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="价格:" prop="goods_price">
          <el-input v-model="addGoodsForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="数量:" prop="goods_number">
          <el-input v-model="addGoodsForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="重量:" prop="goods_weight">
          <el-input v-model="addGoodsForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addGoodsdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoodsRequest">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },

      total: 0,
      addGoodsdialogVisible: false,
      addGoodsForm: {
        goods_name: '',
        goods_cat: '1,2,3',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
      },
      addGoodsFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.getGoodsList()
  },
  computed: {},
  methods: {
    //获取商品列表
    async getGoodsList() {
      const { data: res } = await this.axios.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status != 200) {
        return
      }
      res.data.goods.forEach((item) => {
        let time = new Date(item.add_time)
        item.add_time =
          time.getFullYear() +
          '-' +
          (time.getMonth() + 1) +
          '-' +
          time.getDate() +
          ' ' +
          time.getHours() +
          ':' +
          time.getMinutes() +
          ':' +
          time.getSeconds()
      })
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(res.data)
    },
    //  每页数据的条数发生变化是触发的函数
    handleSizeChange(currentSize) {
      this.queryInfo.pagesize = currentSize
      this.getGoodsList()
    },
    //当前页发生变化是触发的函数
    handleCurrentChange(currentPage) {
      this.queryInfo.pagenum = currentPage
      this.getGoodsList()
    },
    // 显示添加商品的对话框
    showAddGoodsDialog() {
      this.addGoodsdialogVisible = true
    },
    // 发起添加商品的网络请求
    addGoodsRequest() {
      this.$refs.addGoodsFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error('添加商品失败')
        }
        const { data: res } = await this.axios.post('goods', this.addGoodsForm)
        if (res.meta.status != 201) {
          return this.$message.error('添加失败')
        }
        this.$message.success('添加成功')
        this.getGoodsList()
        this.addGoodsdialogVisible = false
        console.log(res)
      })
    },
    // 对添加商品表单进行重置
    addGoodsDialogColsed() {
      this.$refs.addGoodsFormRef.resetFields()
    },
  },
}
</script>

<style></style>
